/**********************************************************************************
 * cxc demo
 * All rights reserved.
 * @author:chenxiaocang
 * 2013-4-23 下午10:40:18
 *@description:
 **********************************************************************************/
package com.cxc.myweb.demo.util;

import java.util.Collection;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class JsonUtils {

	public JsonUtils() {
	}

	public static String toJSON(Object bean) {
		try {
		JSONObject jsonObject = JSONObject.fromObject(bean);
		return jsonObject.toString();
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String toJSON(List list) {
		if (list == null)
			return null;
		try {
			JSONArray jsonObject = JSONArray.fromObject(list);
			return jsonObject.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "[]";
	}

	public static String toJSON(Collection c) {
		if (c == null)
			return null;
		try {
			JSONArray jsonObject = JSONArray.fromObject(c);
			return jsonObject.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "[]";
	}
	
}
